define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'weblog/source_config/index' + location.search,
                    del_url: 'weblog/source_config/del',
                    table: 'source_config',
                    edit_url : 'weblog/source_config/edit',
                    add_url : "weblog/source_config/add"
                }
            });

            let table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'source', title: __('Name')},
                        {field: 'version', title: __('线上版本'),editable: true},
                        {field: 'appid', title: __('appId'),},
                        {field: 'platform', title: __('平台'), },
                        {field: 'page_name', title: __('页面名称'), },
                        {field: 'page_title', title: __('页面标题'), },
                        {field: 'entry_text', title: __('登录入口'), },
                        {field: 'index_style', title: __('首页风格'),  },
                        {field: 'is_ios', title: __('苹果支付提示'), formatter: Table.api.formatter.toggle},
                        {field: 'add_time', title: __('添加时间'),  formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name : "setting",
                                    title : "页面装修",
                                    url : "weblog/source_config/setting"+location.search,
                                    icon : 'fa fa fa-motorcycle',
                                    classname: "btn btn-primary btn-dialog",
                                    text : "页面装修",
                                    extend: `data-area='["80%","80%"]'`
                                },
                                {
                                    name : "setting",
                                    title : "App下载",
                                    url : "weblog/source_config/appDownload"+location.search,
                                    icon : 'fa fa fa-apple',
                                    classname: "btn btn-primary btn-dialog",
                                    text : "App下载",
                                    extend: `data-area='["80%","80%"]'`
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        add : function () {
            Controller.api.bindevent();
        },
        edit: function (){
            Controller.api.bindevent();
        },
        //================ 页面设置
        setting(){
            let self;
            //========= 进行图片的替换
            $(document).on('click','.item-all li',function () {
                let imgs = '';
                let arrs = [];
                $(this).find('img').each(function (index,item) {
                    imgs += "<img src='"+$(item).attr('src')+"'>";
                    arrs.push($(item).attr('src'));
                });
                $(self).find('.icon-select').html(imgs);
                $(self).find('.icons-module').val(arrs.toString());
                Layer.closeAll();
            });
            $(document).on('click','.icon-img',function () {
                self = this;
                let html = '';
                let icons = Config.iconConfig;
                for (const argumentsKey in icons) {
                    let ls = icons[argumentsKey];
                    html += `<li class="await-img"><div>`;
                    for (const iconKey in ls) {
                        html += `<img src="http://vip.niukewang.com/static/icon/${ls[iconKey]}" alt="">`;
                    }
                    html += `</div><p class="title">${argumentsKey}</p></li>`;
                }
                Layer.open({
                    content : `<div><ul class="item-all">${html}</ul></div>`,
                    type : 1,
                    area:["600px","300px"],
                    btn : ['关闭'],
                    yes: function () {
                        Layer.closeAll();
                    }
                });
            });
            //======= 移除
            $(document).on('click','.fa-close',function () {
                $(this).parent().remove();
            });
            $(".plus").click(function () {
                let counter = ((new Date()).getMilliseconds() + Math.random()*1000).toFixed(0);
                let  type = $(this).data('type'),html;
                if (type != 1)
                {
                    html = `<li class="nav-item">
                    <div class="text-center icon-img">
                        <div class="icon-select">
                            <img src="http://vip.niukewang.com/assets/img/avatar.png" alt="" class="img">
                            <img src="http://vip.niukewang.com/assets/img/avatar.png" alt="" class="img">
                        </div>
                        <input type="hidden" name="row[nav][${counter}][icon]" value="" class="icons-module">
                        <p style="font-size: 12px">选择</p>
                    </div>
                    <div>
                        <div>
                            <input type="text" class="form-control" placeholder="请输入导航名称" name="row[nav][${counter}][name]">
                        </div>
                        <div>
                            <select name="row[nav][${counter}][url]" id="" class="form-control">
                                <option value="">请选择页面链接</option>
                                <option value="/pages/index/index">首页</option>
                                <option value="/pages/questionBank/index">题目页</option>
                                <option value="/pages/questionBank/emulation/bagList/bagList?bagType=fee&itemName=押题圈题">套餐</option>
                                <option value="/pages/my/index">我的</option>
                            </select>
                        </div>
                    </div>
                    <span class="fa fa-close"></span>
                </li>`;
                }
                else
                {
                    html = `<li class="nav-item">
                   <div class="text-center icon-img">
                       <div class="icon-select">
                           <img src="http://vip.niukewang.com/assets/img/avatar.png" alt="" class="img">
                           <img src="http://vip.niukewang.com/assets/img/avatar.png" alt="" class="img">
                       </div>
                       <input type="hidden" name="row[module][${counter}][icon]" value="" class="icons-module">
                       <p style="font-size: 12px">选择</p>
                   </div>
                   <div>
                       <div>
                           <input type="text" class="form-control" placeholder="请输入导航名称" name="row[module][${counter}][name]">
                       </div>
                       <div>
                           <select name="row[module][${counter}][url]" id="" class="form-control">
                               <option value="">请选择页面链接</option>
                               <option value="/pages/questionBank/questionBankAnswer/index?id=1&from_type=3&name=免费试题 ">免费试题x</option>
                               <option value="">历年真题</option>
                               <option value="/pages/questionBank/questionBankList/index?from_type=1">章节练习</option>
                               <option value="/pages/questionBank/emulation/emulation">模拟考场</option>
                               <option value="">考试指南</option>
                               <option value="/pages/questionBank/emulation/bagList/bagList?bagType=fee&itemName=押题圈题">套餐</option>
                               <option value="/pages/questionBank/notes/notes">笔记</option>
                               <option value="/pages/my/collect/index">收藏</option>
                           </select>
                       </div>
                   </div>
                   <span class="fa fa-close"></span>
               </li>`;
                }
                $(this).before(html);
                counter++;
            });
            Controller.api.bindevent();
        },
        //====== 方法名称使用case-kabel命名时需要全部使用小写
        appdownload()
        {
            //================== 切换页面内容
            $('input[name="row[download_type]"]').change(function () {
                $.get('weblog/source_config/getDownloadContent?type='+$(this).val(),function (res) {
                    $('.editor').val(res.data);
                });
            });
            Controller.api.bindevent();
        }
    };
    return Controller;
});